<!--
  - Copyright 2011 Angel Sanadinov
  -
  - This file is part of VBox WMI.
  -
  - VBox WMI is free software: you can redistribute it and/or modify
  - it under the terms of the GNU General Public License as published by
  - the Free Software Foundation, either version 3 of the License, or
  - (at your option) any later version.
  -
  - VBox WMI is distributed in the hope that it will be useful,
  - but WITHOUT ANY WARRANTY; without even the implied warranty of
  - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  - GNU General Public License for more details.
  -
  - You should have received a copy of the GNU General Public License
  - along with VBox WMI.  If not, see <http://www.gnu.org/licenses/>.
  -->

<%@tag import="Beans.Responses.DataRetrievalComponents.ServerDataComponent"%>
<%@tag import="Beans.Responses.DataRetrievalResponse"%>
<%@tag import="Utilities.Constants.ResourceRequestType"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="jstlc" %>
<%@tag description="Prints server(s) data." pageEncoding="UTF-8" trimDirectiveWhitespaces="true"%>

<%-- Attributes --%>
<%@attribute name="data" required="true" description="Server(s) data response object."
             rtexprvalue="true" type="DataRetrievalResponse<ServerDataComponent>"%>

<%@attribute name="isOwner" required="true" 
             description="Used for setting up the UI. Non-owners are not allowed to modify servers."
             rtexprvalue="true" type="java.lang.Boolean"%>
<%-- End of Attributes --%>

<%-- Page Body Content --%>
<jstlc:choose>
    <jstlc:when test="${data == null}">
        Failed to retrieve server(s) data object.
    </jstlc:when>

    <jstlc:when test="${data.requestResult && data.valid}">
        <jstlc:choose>
            <jstlc:when test="<%= data.getResourceRequestType() == ResourceRequestType.SINGLE_RESOURCE %>">
                <table id="mainServerTable" class="mainResourceDataTable">
                    <tr><td>Server Id:</td><td>${data.resource.serverId}</td></tr>
                    <tr><td>Server Name:</td><td>${data.resource.serverName}</td></tr>
                    <tr><td>Server Description:</td><td>${data.resource.serverDescription}</td></tr>
                <jstlc:if test="${!data.resource.accessLimited}">
                    <jstlc:set var="server" value="${data.resource.serverData}"/>
                    <tr><td>Addition Date:</td><td>${server.dateAdded}</td></tr>
                    <tr><td>Service Address:</td><td>${server.webServiceAddress}</td></tr>
                    <tr><td>Shared Folders Root:</td><td>${server.sharedFoldersRoot}</td></tr>
                    <tr><td>VRDP Ports Range:</td><td>${server.vrdpPortsRangeLow} - ${server.vrdpPortsRangeHigh}</td></tr>
                    <tr><td>VRDP Ports Per Machine:</td><td>${server.vrdpPortsPerMachine}</td></tr>
                    <tr><td>System Info:</td><td><a class="actionLink" href="system.jsp?id=${server.serverId}">View</a></td></tr>
                    <tr><td>Server Logs:</td><td><a class="actionLink" href="logs.jsp?limit=server&amp;serverId=${server.serverId}">View</a></td></tr>
                </jstlc:if>
                </table>
                <jstlc:if test="${isOwner}">
                <div id="serverOperations">
                    <a class="actionLink" href="noJavaScript.jsp" onclick="return changePopupVisibility('formModifyServer')">Modify Server</a>
                </div>
                </jstlc:if>
                <form class="resourceModification" id="formModifyServer" action="manage_server.jsp?action=2&amp;id=${data.resource.serverId}" method="post">
                    <table>
                        <tr><td>Server Name:</td><td><input type="text" name="modify.name" value="${data.resource.serverName}"></td></tr>
                        <tr><td>Server Description:</td><td><input type="text" name="modify.description" value="${data.resource.serverDescription}"></td></tr>
                        <tr><td>Service Address:</td><td><input type="text" name="modify.wsAddress" value="${server.webServiceAddress}"></td></tr>
                        <tr><td>Shared Folders Root:</td><td><input type="text" name="modify.sfRoot" value="${server.sharedFoldersRoot}"></td></tr>
                        <tr><td>VRDP Ports Range:</td><td><input size="8" type="text" name="modify.portsLow" value="${server.vrdpPortsRangeLow}">
                                - <input size="8" type="text" name="modify.portsHigh" value="${server.vrdpPortsRangeHigh}"></td></tr>
                        <tr><td>VRDP Ports Per Machine:</td><td><input size="5" type="text" name="modify.portsPerMachine" value="${server.vrdpPortsPerMachine}"></td></tr>
                        <tr><td>Warning:</td><td>Modifying the VRDP port range<br>and/or ports per machine
                                                 <br>can lead to inconsistent allocation<br>of slots for new machines.</td></tr>
                        <tr><td>Service Username:</td><td><input type="text" name="modify.wsUser"></td></tr>
                        <tr><td>Service Password:</td><td><input type="password" name="modify.wsPass"></td></tr>
                        <tr><td><input type="submit" value="Update"></td></tr>
                    </table>
                </form>
            </jstlc:when>
            <jstlc:when test="<%= data.getResourceRequestType() == ResourceRequestType.MULTIPLE_RESOURCES %>">
                <table id="serversTable" class="resourcesTable">
                    <tr><td>Server ID</td><td>Name</td><td>Description</td><td></td><td></td><td></td></tr>
                    <jstlc:forEach var="server" items="${data.resourcesList}">
                        <tr><td>${server.serverId}</td><td>${server.serverName}</td><td>${server.serverDescription}</td>
                            <td><a class="operationsLink" href="servers.jsp?id=${server.serverId}">View</a></td>
                            <td><jstlc:if test="${isOwner}"><a class="operationsLink" href="manage_server.jsp?id=${server.serverId}&amp;action=3"
                                   onclick="return resourceRemovalVerification('${server.serverName}', this);">Remove</a></jstlc:if></td>
                            <td><a class="operationsLink" href="servers.jsp?change=${server.serverId}">Select</a></td></tr>
                    </jstlc:forEach>
                </table>
                <script type="text/javascript">
                    var serversPagination = new ElementsTablePagination('serversTable', 'serversPagination');
                </script>
                Page Size: <input type="text" size="2" id="itemsPerPage" value="20">
                <a class="pageSizeUpdateLink" href="noJavaScript.jsp"
                   onclick="return serversPagination.setMaxNumberOfItemsPerPage(document.getElementById('itemsPerPage').value);">Update</a>
                <jstlc:if test="${isOwner}">
                <div class="creationOperations">
                <a class="creationLink" href="noJavaScript.jsp" onclick="return changePopupVisibility('formCreateServer');">Add Server</a>
                <form class="resourceCreationElement" id="formCreateServer" action="manage_server.jsp?action=1" method="post">
                    <table>
                        <tr><td>Create Server:</td></tr>
                        <tr><td>Server Name:</td><td><input type="text" name="create.name"></td></tr>
                        <tr><td>Server Description:</td><td><input type="text" name="create.description"></td></tr>
                        <tr><td>Service Address:</td><td><input type="text" name="create.wsAddress"></td></tr>
                        <tr><td>Service Username:</td><td><input type="text" name="create.wsUser"></td></tr>
                        <tr><td>Service Password:</td><td><input type="password" name="create.wsPass"></td></tr>
                        <tr><td>Shared Folders Root:</td><td><input type="text" name="create.sfRoot"></td></tr>
                        <tr><td>VRDP Ports Range:</td><td><input size="8" type="text" name="create.portsLow">
                                                          - <input size="8" type="text" name="create.portsHigh"></td></tr>
                        <tr><td>VRDP Ports Per Machine:</td><td><input size="5" type="text" name="create.portsPerMachine"></td></tr>
                        <tr><td><input type="submit" value="Create"></td></tr>
                    </table>
                </form>
                </div>
                </jstlc:if>
            </jstlc:when>

            <jstlc:otherwise>
                Invalid resource request type: ${data.resourceRequestType}.
            </jstlc:otherwise>
        </jstlc:choose>
    </jstlc:when>

    <jstlc:otherwise>
        Error: ${data.exception.exceptionMessage}
    </jstlc:otherwise>
</jstlc:choose>
<%-- End of Page Body Content --%>